package com.getpebble.android.framework.appmessage;

import android.util.Base64;
import com.sun.mail.imap.protocol.ListInfo;
import com.sun.mail.smtp.SMTPMessage;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class PebbleTuple {
    public static Map<Byte, String> AsciiReadableInterpretMap = makeAsciiReadableInterpretMap();
    public static final Map<String, TupleType> TYPE_NAMES = new HashMap();
    public static final TupleType[] TYPE_ORDINALS = new TupleType[]{TupleType.BYTES, TupleType.STRING, TupleType.UINT, TupleType.INT};
    public static Map<Byte, String> ValueLiteralInterpretMap = makeValueLiteralInterpretMap();
    public static final Map<Integer, Width> WIDTH_MAP = new HashMap();
    public final int key;
    public final int length;
    public final TupleType type;
    public final Object value;
    public final Width width;

    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$TupleType = new int[TupleType.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$TupleType[TupleType.BYTES.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$TupleType[TupleType.STRING.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$TupleType[TupleType.INT.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$TupleType[TupleType.UINT.ordinal()] = 4;
            } catch (NoSuchFieldError e4) {
            }
        }
    }

    public enum TupleType {
        BYTES(0),
        STRING(1),
        UINT(2),
        INT(3);
        
        public final byte ord;

        private TupleType(int ord) {
            this.ord = (byte) ord;
        }

        public String getName() {
            return name().toLowerCase();
        }
    }

    public static class ValueOverflowException extends RuntimeException {
        public ValueOverflowException() {
            super("Value exceeds tuple capacity");
        }
    }

    public enum Width {
        NONE(0),
        BYTE(1),
        SHORT(2),
        WORD(4);
        
        public final int value;

        private Width(int width) {
            this.value = width;
        }
    }

    static {
        for (TupleType t : TupleType.values()) {
            TYPE_NAMES.put(t.getName(), t);
        }
        for (Width w : Width.values()) {
            WIDTH_MAP.put(Integer.valueOf(w.value), w);
        }
    }

    private PebbleTuple(int key, TupleType type, Width width, int length, Object value) {
        this.key = key;
        this.type = type;
        this.width = width;
        this.length = length;
        this.value = value;
    }

    public static PebbleTuple create(int key, TupleType type, Width width, int value) {
        return new PebbleTuple(key, type, width, width.value, Long.valueOf((long) value));
    }

    public static PebbleTuple create(int key, TupleType type, Width width, Object value) {
        int length = Integer.MAX_VALUE;
        if (width != Width.NONE) {
            length = width.value;
        } else if (type == TupleType.BYTES) {
            length = ((byte[]) value).length;
        } else if (type == TupleType.STRING) {
            length = ((String) value).getBytes(Charset.forName("UTF-8")).length + 1;
        }
        if (length <= 65535) {
            return new PebbleTuple(key, type, width, length, value);
        }
        throw new ValueOverflowException();
    }

    public JSONObject toJson() {
        JSONObject jSONObject = null;
        try {
            jSONObject = serializeTuple(this, null);
        } catch (Exception e) {
        }
        return jSONObject;
    }

    public String toJsonString() {
        JSONObject jsonObject = toJson();
        return jsonObject == null ? "{}" : jsonObject.toString();
    }

    public String toString() {
        return toJsonString();
    }

    public static JSONObject serializeTuple(PebbleTuple t) throws JSONException {
        JSONObject j = new JSONObject();
        j.put("key", t.key);
        j.put("type", t.type.getName());
        j.put("length", t.width.value);
        switch (AnonymousClass1.$SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$TupleType[t.type.ordinal()]) {
            case SMTPMessage.RETURN_FULL /*1*/:
                j.put("value", Base64.encodeToString((byte[]) t.value, 2));
                break;
            case SMTPMessage.RETURN_HDRS /*2*/:
                j.put("value", t.value);
                break;
            case ListInfo.INDETERMINATE /*3*/:
            case SMTPMessage.NOTIFY_DELAY /*4*/:
                j.put("value", t.value);
                break;
        }
        return j;
    }

    public static JSONObject serializeTuple(PebbleTuple t, Map<Byte, String> interpretMap) throws JSONException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("key", t.key);
        jsonObject.put("type", t.type.getName());
        jsonObject.put("length", t.width.value);
        switch (AnonymousClass1.$SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$TupleType[t.type.ordinal()]) {
            case SMTPMessage.RETURN_FULL /*1*/:
                JSONArray jsonArray = new JSONArray();
                for (byte v : (byte[]) t.value) {
                    if (interpretMap != null) {
                        String sv = (String) interpretMap.get(Byte.valueOf(v));
                        if (sv != null) {
                            jsonArray.put(sv);
                        }
                    }
                    jsonArray.put(ValueLiteralInterpretMap.get(Byte.valueOf(v)));
                }
                jsonObject.put("value", jsonArray);
                break;
            case SMTPMessage.RETURN_HDRS /*2*/:
                jsonObject.put("value", t.value);
                break;
            case ListInfo.INDETERMINATE /*3*/:
            case SMTPMessage.NOTIFY_DELAY /*4*/:
                jsonObject.put("value", t.value);
                break;
        }
        return jsonObject;
    }

    public static Map<Byte, String> makeValueLiteralInterpretMap() {
        Map<Byte, String> rv = new HashMap();
        for (int i = 0; i < 256; i++) {
            rv.put(Byte.valueOf((byte) i), "" + i);
        }
        return rv;
    }

    public static Map<Byte, String> makeAsciiReadableInterpretMap() {
        Map<Byte, String> rv = makeValueLiteralInterpretMap();
        for (int i = 32; i < 127; i++) {
            rv.put(Byte.valueOf((byte) i), new String(new byte[]{(byte) i}));
        }
        return rv;
    }
}
